

/** Projectile object class that has
 * fields for all of the attributes
 * that the Projectile will need.
 * Also has a reverse field
 * for reversing the sprite.
 * 
 * 
 * @author Andrew
 * LAST UPDATED - Andrew 3/02/12
 */

public class ProjectileObject extends Object {
    
    // Standard sprite
    private String sprite = "projectile.png";
    
    // Angle with which the projectile was fired
    private double angle;
    
    // Velocity with which the projectile was fired
    private double vel;
    
    // Time (for now its just an increasing number)
    private double t = 0;
    
    // gravity
    private double g = 0.05;
    
    // Initial x position
    private double xi;
    
    // Initial y position
    private double yi;
    
    
    // create a new projectile, starting from x,y with picture
    // sprite, velocity v and angle.
  public ProjectileObject(double x, double y, double v, double angler) {
      super((new Sprite("projectile.png")),x,y);
      this.vel = v;
      this.angle = angler; // Convert to radians for Math.trig
      this.xi = x;
      this.yi = y;
  }
  
  // Trajectory Equation linking X position with time
  private double trajectoryX(double v,double t) {
      double output = v * t;
      return output;
  }
    
  // Trajectory Equation for Y position as a function of time
  private double trajectoryY(double v, double t, double g) {
      double output = (v * t) - (1 * g * t * t);
      return output;
  }
  
  // Calculates the next position, moves, draws and then increases time.
  public void update() {
      // Velocity in x (angle to radian conversion)
      double velX = vel * Math.cos(angle * 0.0174532925);
      // Velocity in y
      double velY = vel * Math.sin(angle * 0.0174532925);
      
      // Set the new x and y positions relative to the 
      // Starting point
      setX(trajectoryX(velX,t) + xi);
      setY(trajectoryY(velY,t,g) + yi);
      
      // increase time by 1
      this.t = t + 1;
      // draw the projectile in its new position.
      this.draw();
  }
  
  
  // Sets the x position of the projectile
  public void setX(double n) {
      x = n;
  }
  
  // Sets the y position of the projectile
  public void setY(double n) {
      this.y = n;
  }
  
  
  // Resets the missiles time value to 0,
  // its x and y positions back to 0
  public void reset() {
      this.t = 0;
      super.x = 0;
      super.y = 0;
  }

  // Sets the projectiles velocity at which it was fired
  public void setVelocity(double v) {
      this.vel = v;
  }
  
  // Increases time manually
  public void increaseTime() {
      t = t+1;
  }
  
  // Returns the objects current time
  public double getTime() {
      return t;
  }
  
  // Sets the Angle at which it was fired
  public void setAngle(double angle) {
      this.angle = angle;
  }
  
}